package cn.ghf.quartz.service.impl;

import cn.ghf.basic.constant.PayBillBusinessType;
import cn.ghf.basic.util.QuartzJobInfo;
import cn.ghf.pay.domain.PayBill;
import cn.ghf.pay.mapper.PayBillMapper;
import cn.ghf.quartz.job.MainJob;
import cn.ghf.quartz.service.IQuartzService;
import cn.ghf.quartz.util.QuartzUtils;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;

@Service
public class QuartzServiceImpl implements IQuartzService {

    @Autowired
    private PayBillMapper payBillMapper;
    @Autowired
    private SchedulerFactoryBean factoryBean;
    /**
     * 添加定时任务
     * @param info
     */
    @Override
    public void addJob(QuartzJobInfo info) {
        String paySn = info.getParams().get("paySn").toString();
        PayBill payBill = payBillMapper.loadByUnionPaySn(paySn);
        if(PayBillBusinessType.BUSINESS_TYPE_ADOPT.equals(payBill.getBusinessType())){
            Scheduler scheduler = factoryBean.getScheduler();
            QuartzUtils.addJob(scheduler, info.getJobName(), MainJob.class, info, info.getCronj());
        }

    }

    /**
     * 订单已付款，删除job
     * @param unionPaySn
     */
    @Override
    public void remove(String unionPaySn) {
        QuartzUtils.removeJob(factoryBean.getScheduler(), unionPaySn);
    }
}
